package pl.vanta.mjuzik.gui.panels;

import java.awt.Color;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JTextField;

import pl.vanta.mjuzik.model.Options;
import pl.vanta.utils.gui.buttons.VColorButton;
import pl.vanta.utils.gui.dialogs.AbstractDialogContainer;
import pl.vanta.utils.gui.panels.LabelValuePanel;

public class OptionsPanel extends AbstractDialogContainer<Options>
{
	private static final long	serialVersionUID	= 1L;

	private JCheckBox			cbxSortOnLoad;
	private JCheckBox			cbxCyclePlaylist;
	private JCheckBox			cbxLazyPlaylistLoading;
	private JCheckBox			cbxCheckFilePresenceOnLoad;
	private JCheckBox			cbxShuffle;
	private VColorButton		colorButton;
	private JTextField			txtTimeFormat;

	public OptionsPanel()
	{
		initComponents();
		layoutComponents();
	}

	public void initComponents()
	{
		this.cbxCheckFilePresenceOnLoad = new JCheckBox("Check file presence on load");
		this.cbxCyclePlaylist = new JCheckBox("Cycle playlist");
		this.cbxLazyPlaylistLoading = new JCheckBox("Lazy playlist loading");
		this.cbxShuffle = new JCheckBox("Shuffle");
		this.cbxSortOnLoad = new JCheckBox("Sort on load");

		this.colorButton = new VColorButton(Color.BLACK);
		this.txtTimeFormat = new JTextField();
	}

	public void layoutComponents()
	{
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		this.add(this.cbxCheckFilePresenceOnLoad);
		this.add(this.cbxCyclePlaylist);
		this.add(this.cbxLazyPlaylistLoading);
		this.add(this.cbxShuffle);
		this.add(this.cbxSortOnLoad);
		this.add(new LabelValuePanel("Playing file background:", this.colorButton, false));
		this.add(new LabelValuePanel("Time format:", this.txtTimeFormat));
	}

	@Override
	public Options getDialogBean()
	{
		Options c = new Options();

		c.setCheckFilePresenceOnLoad(this.cbxCheckFilePresenceOnLoad.isSelected());
		c.setCyclePlaylist(this.cbxCyclePlaylist.isSelected());
		c.setLazyPlaylistLoading(this.cbxLazyPlaylistLoading.isSelected());
		c.setSortOnLoad(this.cbxSortOnLoad.isSelected());
		c.setShuffle(this.cbxShuffle.isSelected());
		c.setPlayingFileSelectionColor(this.colorButton.getColor());
		c.getTimeFormat().setPattern(this.txtTimeFormat.getText());

		return c;
	}

	@Override
	public void setDialogBean(Options bean)
	{
		this.cbxCheckFilePresenceOnLoad.setSelected(bean.isCheckFilePresenceOnLoad());
		this.cbxCyclePlaylist.setSelected(bean.isCyclePlaylist());
		this.cbxLazyPlaylistLoading.setSelected(bean.isLazyPlaylistLoading());
		this.cbxShuffle.setSelected(bean.isShuffle());
		this.cbxSortOnLoad.setSelected(bean.isSortOnLoad());
		this.colorButton.setColor(bean.getPlayingFileSelectionColor());
		this.txtTimeFormat.setText(bean.getTimeFormat().getPattern());
	}

	@Override
	public boolean canClose()
	{
		return true;
	}

	@Override
	public String getTitle()
	{
		return "mJuzik options";
	}
}